package com.lframework.starter.web.inner.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.inner.entity.OrderTimeLine;
import com.lframework.starter.web.inner.mappers.OrderTimeLineMapper;
import com.lframework.starter.web.inner.service.OrderTimeLineService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author zmj
 * @since 2022/8/10
 */
@Service
public class OrderTimeLineServiceImpl extends
        BaseMpServiceImpl<OrderTimeLineMapper, OrderTimeLine> implements
        OrderTimeLineService {

    @Cacheable(value = OrderTimeLine.CACHE_NAME, key = "@cacheVariables.tenantId() + #orderId", unless = "#result == null")
    @Override
    public List<OrderTimeLine> getByOrder(String orderId) {
        Wrapper<OrderTimeLine> queryWrapper = Wrappers.lambdaQuery(OrderTimeLine.class).eq(OrderTimeLine::getOrderId, orderId).orderByAsc(OrderTimeLine::getCreateTime);

        return this.list(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByOrder(String orderId) {
        Wrapper<OrderTimeLine> deleteWrapper = Wrappers.lambdaQuery(OrderTimeLine.class).eq(OrderTimeLine::getOrderId, orderId);
        this.remove(deleteWrapper);
    }

    @CacheEvict(value = OrderTimeLine.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {
        super.cleanCacheByKey(key);
    }
}
